package com.cpe.web.webinitializer;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.core.annotation.Order;
import org.springframework.web.WebApplicationInitializer;

import com.thetransactioncompany.cors.CORSConfigurationLoader;
import com.thetransactioncompany.cors.CORSFilter;

@Order(1)
public class BerforeWebApplicationInitialer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		// 注册CORS过滤器
		FilterRegistration.Dynamic dynamic = servletContext.addFilter("CORS", CORSFilter.class);
		dynamic.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
		dynamic.setInitParameter(CORSConfigurationLoader.CONFIG_FILE_PARAM_NAME, "/cors.properties");
	}

}
